package com.tanhua.dubbo.api;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tanhua.domain.db.BlackList;
import com.tanhua.domain.db.UserInfo;
import com.tanhua.dubbo.mapper.BlackListMapper;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

@Service
public class BlackListApiImpl implements BlackListApi {
    @Autowired
    private BlackListMapper blackListMapper;

    @Override
    public IPage<UserInfo> findByUserId(Integer page, Integer pagesize, Long userId) {
        return blackListMapper.findBlackList(new Page<>(page, pagesize), userId);
    }

    @Override
    public void remove(Long userId, Long blackUserId) {
        LambdaQueryWrapper<BlackList> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(BlackList::getUserId, userId);
        wrapper.eq(BlackList::getBlackUserId, blackUserId);
        blackListMapper.delete(wrapper);
    }

    /**
     * 获取当前用户的黑名单id有序集合
     *
     * @param userId
     * @return
     */
    @Override
    public List<Long> findBlackListIdsListByUserId(Long userId) {
        LambdaQueryWrapper<BlackList> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(BlackList::getUserId, userId);
        List<BlackList> blackListList = blackListMapper.selectList(wrapper);
        if (blackListList == null || blackListList.size() == 0) {
            return null;
        }
        List<Long> blackListIdList = new ArrayList<>();
        for (BlackList blackList : blackListList) {
            blackListIdList.add(blackList.getBlackUserId());
        }
        return blackListIdList;
    }

    /**
     * 拉黑了当前用户的id的集合
     *
     * @param currentUserId
     * @return
     */
    @Override
    public List<Long> findListBlackCurrentUser(Long currentUserId) {
        LambdaQueryWrapper<BlackList> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(BlackList::getBlackUserId, currentUserId);
        List<BlackList> blackMeList = blackListMapper.selectList(wrapper);
        if (blackMeList == null || blackMeList.size() == 0) {
            return null;
        }
        List<Long> blackMeIdList = new ArrayList<>();
        for (BlackList blackList : blackMeList) {
            blackMeIdList.add(blackList.getUserId());
        }
        return blackMeIdList;
    }
}
